﻿using System.Collections.Generic;
using StockLib.UserData;
using System.Drawing;

namespace StockLib
{
    public class StockColorSetting
    {
        public Color BackColor;
        public Color UpLineColor;
        public Color UpTextColor;
        public Color DnLineColor;
        public Color DnTextColor;
        public Color StLineColor;
        public Color StTextColor;
        public Color AxisLineColor;
        public Color AxisTextColor;
        public Color GridLineColor;
        public Color GridSelectedLineColor;
        public Color GridTextColor;
        public Color GridHiTextColor;
        public Color GridHeadBackColor;
        public Color GridHeadTextColor;
        public Color GridTabBackColor;
        public Color GridTabTextColor;
        public Color GridTabHiBackColor;
        public Color GridTabHiTextColor;
        public Color IndicatorColor1;
        public Color IndicatorColor2;
        public Color IndicatorColor3;
        public Color IndicatorColor4;
        public Color IndicatorColor5;
        public Color IndicatorColor6;
        public Color GraphTextColor;
        public Color VolumeTextColor;
    }

    public class StockPenSetting
    {
        public Pen UpLinePen;
        public Pen DnLinePen;
        public Pen StLinePen;
        public Pen AxisLinePen;
        public Pen GridLinePen;
        public Pen GridSelectedLinePen;
        public Pen IndicatorPen1;
        public Pen IndicatorPen2;
        public Pen IndicatorPen3;
        public Pen IndicatorPen4;
        public Pen IndicatorPen5;
        public Pen IndicatorPen6;
    }

    public class StockBrushSetting
    {
        public Brush BackBrush;
        public Brush UpLineBrush;
        public Brush DnLineBrush;
        public Brush StLineBrush;
    }

    public class StockFontSetting
    {
        public Font DefaultFont;
        public Font GridFont;
        public Font GraphFont;
        public Font InfoFont;
    }

    public class StockProfile
    {
        private List<StockMarket> _markets = new List<StockMarket>();

        private int _cacheDays = 0;

        private StockColorSetting _colorSetting = new StockColorSetting();

        private StockPenSetting _penSetting = new StockPenSetting();

        private StockBrushSetting _brushSetting = new StockBrushSetting();

        private StockFontSetting _fontSetting = new StockFontSetting();

        private Color[] _colorArray = new Color[6];

        private Pen[] _penArray = new Pen[6];

        /// <summary>
        /// 单例实例
        /// </summary>
        private static StockProfile _instance = new StockProfile();

        /// <summary>
        /// 构造函数
        /// </summary>
        private StockProfile()
        {
            LoadDefault();
        }

        /// <summary>
        /// 析构函数
        /// </summary>
        ~StockProfile()
        {
            Reset();
        }

        /// <summary>
        /// 访问单例
        /// </summary>
        /// <returns></returns>
        public static StockProfile Instance
        {
            get
            {
                return _instance;
            }
        }

        private void Reset()
        {
            _markets.Clear();
            _cacheDays = 0;

            if (_penSetting.UpLinePen != null)
                _penSetting.UpLinePen.Dispose();
            if (_penSetting.DnLinePen != null)
                _penSetting.DnLinePen.Dispose();
            if (_penSetting.StLinePen != null)
                _penSetting.StLinePen.Dispose();
            if (_penSetting.AxisLinePen != null)
                _penSetting.AxisLinePen.Dispose();
            if (_penSetting.GridLinePen != null)
                _penSetting.GridLinePen.Dispose();
            if (_penSetting.GridSelectedLinePen != null)
                _penSetting.GridSelectedLinePen.Dispose();
            if (_penSetting.IndicatorPen1 != null)
                _penSetting.IndicatorPen1.Dispose();
            if (_penSetting.IndicatorPen2 != null)
                _penSetting.IndicatorPen2.Dispose();
            if (_penSetting.IndicatorPen3 != null)
                _penSetting.IndicatorPen3.Dispose();
            if (_penSetting.IndicatorPen4 != null)
                _penSetting.IndicatorPen4.Dispose();
            if (_penSetting.IndicatorPen5 != null)
                _penSetting.IndicatorPen5.Dispose();
            if (_penSetting.IndicatorPen6 != null)
                _penSetting.IndicatorPen6.Dispose();

            if (_brushSetting.BackBrush != null)
                _brushSetting.BackBrush.Dispose();
            if (_brushSetting.UpLineBrush != null)
                _brushSetting.UpLineBrush.Dispose();
            if (_brushSetting.DnLineBrush != null)
                _brushSetting.DnLineBrush.Dispose();
            if (_brushSetting.StLineBrush != null)
                _brushSetting.StLineBrush.Dispose();
        }

        private void LoadDefault()
        {
            Reset();

            _markets.Add(StockMarket.SHSE);
            _markets.Add(StockMarket.SZSE);

            _colorSetting.BackColor = Color.Black;
            _colorSetting.UpLineColor = Color.FromArgb(255, 50, 50);
            _colorSetting.UpTextColor = Color.FromArgb(255, 50, 50);
            _colorSetting.DnLineColor = Color.FromArgb(84, 255, 255);
            _colorSetting.DnTextColor = Color.FromArgb(0, 200, 0); //Color.FromArgb(0, 255, 0x66);
            _colorSetting.StLineColor = Color.White;
            _colorSetting.StTextColor = Color.White;
            _colorSetting.AxisLineColor = Color.FromArgb(176, 0, 0);
            _colorSetting.AxisTextColor = Color.FromArgb(255, 50, 50);
            _colorSetting.GridTextColor = Color.FromArgb(192, 192, 192); //Color.White;
            _colorSetting.GridHiTextColor = Color.FromArgb(240, 248, 136); //Color.Gold;
            _colorSetting.GridHeadBackColor = Color.FromArgb(60, 60, 60);
            _colorSetting.GridHeadTextColor = Color.FromArgb(221, 221, 221);
            _colorSetting.GridTabBackColor = Color.FromArgb(0x22, 0x22, 0x22);
            _colorSetting.GridTabTextColor = Color.FromArgb(221, 221, 221);
            _colorSetting.GridTabHiBackColor = Color.FromArgb(0x55, 0, 0);
            _colorSetting.GridTabHiTextColor = Color.FromArgb(255, 255, 255);
            _colorSetting.GridLineColor = Color.Black;
            _colorSetting.GridSelectedLineColor = Color.Red;
            _colorSetting.IndicatorColor1 = Color.FromArgb(250, 250, 250);
            _colorSetting.IndicatorColor2 = Color.FromArgb(250, 250, 0);
            _colorSetting.IndicatorColor3 = Color.FromArgb(250, 0, 250);
            _colorSetting.IndicatorColor4 = Color.FromArgb(0, 250, 0);
            _colorSetting.IndicatorColor5 = Color.FromArgb(192, 192, 192);
            _colorSetting.IndicatorColor6 = Color.FromArgb(0, 0, 250);
            _colorSetting.GraphTextColor = Color.FromArgb(192, 192, 192);
            _colorSetting.VolumeTextColor = Color.FromArgb(250, 250, 0);

            _fontSetting.DefaultFont = new Font("Tahoma", 8, FontStyle.Regular);
            _fontSetting.GridFont = new Font("Tahoma", 8, FontStyle.Regular);
            _fontSetting.GraphFont = new Font("Tahoma", 8, FontStyle.Regular);
            _fontSetting.InfoFont = new Font("Tahoma", 8, FontStyle.Regular);

            _penSetting.UpLinePen = new Pen(_colorSetting.UpLineColor);
            _penSetting.DnLinePen = new Pen(_colorSetting.DnLineColor);
            _penSetting.StLinePen = new Pen(_colorSetting.StLineColor);
            _penSetting.AxisLinePen = new Pen(_colorSetting.AxisLineColor);
            _penSetting.GridLinePen = new Pen(_colorSetting.GridLineColor);
            _penSetting.GridSelectedLinePen = new Pen(_colorSetting.GridSelectedLineColor);
            _penSetting.IndicatorPen1 = new Pen(_colorSetting.IndicatorColor1);
            _penSetting.IndicatorPen2 = new Pen(_colorSetting.IndicatorColor2);
            _penSetting.IndicatorPen3 = new Pen(_colorSetting.IndicatorColor3);
            _penSetting.IndicatorPen4 = new Pen(_colorSetting.IndicatorColor4);
            _penSetting.IndicatorPen5 = new Pen(_colorSetting.IndicatorColor5);
            _penSetting.IndicatorPen6 = new Pen(_colorSetting.IndicatorColor6);

            _brushSetting.BackBrush = new SolidBrush(_colorSetting.BackColor);
            _brushSetting.UpLineBrush = new SolidBrush(_colorSetting.UpLineColor);
            _brushSetting.DnLineBrush = new SolidBrush(_colorSetting.DnLineColor);
            _brushSetting.StLineBrush = new SolidBrush(_colorSetting.StLineColor);

            _colorArray[0] = _colorSetting.IndicatorColor1;
            _colorArray[1] = _colorSetting.IndicatorColor2;
            _colorArray[2] = _colorSetting.IndicatorColor3;
            _colorArray[3] = _colorSetting.IndicatorColor4;
            _colorArray[4] = _colorSetting.IndicatorColor5;
            _colorArray[5] = _colorSetting.IndicatorColor6;

            _penArray[0] = _penSetting.IndicatorPen1;
            _penArray[1] = _penSetting.IndicatorPen2;
            _penArray[2] = _penSetting.IndicatorPen3;
            _penArray[3] = _penSetting.IndicatorPen4;
            _penArray[4] = _penSetting.IndicatorPen5;
            _penArray[5] = _penSetting.IndicatorPen6;
        }

        public static List<StockMarket> Markets
        {
            get { return Instance._markets; }
        }

        public static int CacheDays
        {
            get { return Instance._cacheDays; }
        }

        public static StockFontSetting Fonts
        {
            get { return Instance._fontSetting; }
        }

        public static StockColorSetting Colors
        {
            get { return Instance._colorSetting; }
        }

        public static StockPenSetting Pens
        {
            get { return Instance._penSetting; }
        }

        public static StockBrushSetting Brushes
        {
            get { return Instance._brushSetting; }
        }

        public static Color[] IndicatorColors
        {
            get { return Instance._colorArray; }
        }

        public static Pen[] IndicatorPens
        {
            get { return Instance._penArray; }
        }

        public virtual void Load()
        {
            // 载入Profile数据
            StockUserData.Instance.LoadProfileData();
        }

        public virtual void Save()
        {
            // 保存Profile数据
            StockUserData.Instance.SaveProfileData();
        }
    }
}
